<?php
class model_send extends model_base{
    
    /**
     * @info 发送客服文本消息(用户与公众号最后交互的48小时内)
     * @author Jackie Yang
     * @date 20140716
     * @param msg  $a = "<a href='http://baidu.com'>链接</a>";
     */
    public static function send_text($openid='',$msg=''){
        if(empty($openid) || empty($msg)){
            framework_static_function::write_log('openid or msg is empty!','weixin_send_text');
            return false;
        }
        $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?';
        $url .= 'access_token='.model_weixin::get_access_token();
        $data['touser'] = $openid;
        $data['msgtype'] = 'text';
        $data['text']['content'] = urlencode($msg);
        $data = urldecode(json_encode($data));
        $res = model_weixin::http_post($url,$data);
        $info = json_decode($res,true);
        if(empty($info)||isset($info['errcode'])) {
        	if($info['errcode'] == 42001) { //access_token过期
            		$access_token = model_weixin::get_access_token_by_weixin();//重新获取access_token
            		framework_static_function::write_log($access_token,'access_token');
            		self::send_text($openid,$msg);
            	} else {
            		framework_static_function::write_log('发送文本消息错误:'.json_encode($info),'weixin_send');
            		return FALSE;
            	}
        }
        framework_static_function::write_log($data,'weixin_send');
        framework_static_function::write_log($res,'weixin_send');
        return $res;
    }
    
    
    /**
     * @info 发送图文消息（单图文）
     * @author Jackie Yang
     * @date 20140719
     * @param openid 微信的用户openid
     * @param title  消息标题
     * @param content 消息内容  换行使用“\r\n”
     * @param url   跳转url地址
     * @param picurl  图片的url地址   绝对路径
     */
    public static function tuwenpost($openid,$title,$content,$url,$picurl=''){
        if(empty($openid)) return false;
        $data['touser'] = $openid;
        $data['msgtype'] = 'news';
        $articles['title'] = urlencode($title);
        $articles['description']=urlencode($content);
        $articles['url'] = urlencode($url);
        if(!empty($picurl)){
            $articles['picurl'] = $picurl;    
        }
        $data['news']['articles'][0] = $articles;
        $data = urldecode(json_encode($data));
        $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?';
        $url .= 'access_token='.model_weixin::get_access_token();
        $res = model_weixin::http_post($url,$data);
        $info = json_decode($res,true);
        if(empty($info)||isset($info['errcode'])) {
        	if($info['errcode'] == 42001) { //access_token过期
        		$access_token = model_weixin::get_access_token_by_weixin();//重新获取access_token
        		framework_static_function::write_log($access_token,'access_token');
        		self::tuwenpost($openid,$title,$content,$url,$picurl);
        	} else {
        		framework_static_function::write_log('发送图文消息错误:'.json_encode($info),'weixin_send');
        		return FALSE;
        	}
        }
        framework_static_function::write_log($data,'weixin_send');
        framework_static_function::write_log($res,'weixin_send');
        return $res;
    }
    /**
     * @author Jackie Yang
     * @date 20140720
     * @param string $openid  接收者openid
     * @param array $articles  图文消息列表
     * 消息报文json格式如下：
     * {
	 *	    "touser":"OPENID",
	 *	    "msgtype":"news",
	 *	    "news":{
	 *	        "articles": [
	 *	         {
	 *	             "title":"Happy Day",
	 *	             "description":"Is Really A Happy Day",
	 *	             "url":"URL",
	 *	             "picurl":"PIC_URL"
	 *	         },
	 *	         {
	 *	             "title":"Happy Day",
	 *	             "description":"Is Really A Happy Day",
	 *	             "url":"URL",
	 *	             "picurl":"PIC_URL"
	 *	         }
	 *	         ]
	 *	    }
	 *	}
     * @description 发送多图文消息接口
     */
    public static function send_news($openid,$articles) {
    	if(empty($openid) || is_array($articles) || count($articles) < 1) {
    		return false;
    	}
    	$data['touser'] = $openid;
    	$data['msgtype'] = 'news';
    	$data['news']['articles'] = json_encode($articles);
    	$message = json_encode($data);
    	$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?';
    	$url .= 'access_token='.model_weixin::get_access_token();
    	$res = model_weixin::http_post($url,$message);
    	$info = json_decode($res,true);
    	if(empty($info)||isset($info['errcode'])) {
    		if($info['errcode'] == 42001) { //access_token过期
    			$access_token = model_weixin::get_access_token_by_weixin();//重新获取access_token
    			framework_static_function::write_log($access_token,'access_token');
    			self::send_news($openid,$articles);
    		} else {
    			framework_static_function::write_log('发送图文消息错误:'.json_encode($info),'weixin_send');
    			return FALSE;
    		}
    	}
    	framework_static_function::write_log($message,'weixin_send');
    	framework_static_function::write_log($res,'weixin_send');
    	return $res;
    }
    
}